import json
import subprocess
import requests
import re
headers={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
    'referer': 'https://www.bilibili.com/'
}
url = 'https://www.bilibili.com/video/BV1wF411g7Xg?'
response = requests.get(url=url,headers=headers).text
result = re.compile('<script>window.__playinfo__=(.*?)</script>',re.S).findall(response)
# result = re.findall('<script>window.__playinfo__=(.*?)</script>',response)
json_data = json.loads(result[0])
video_url = json_data['data']['dash']['video'][0]['baseUrl']
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
video= requests.get(url=video_url,headers=headers).content
audio = requests.get(url=audio_url,headers=headers).content
with open('视频.mp4','wb') as f:
    f.write(video)
with open('视频.mp3','wb') as f:
    f.write(audio)

subprocess.run("ffmpeg -i 视频.mp4 -i 视频.mp3 -c:v copy -c:a copy -bsf:a aac_adtstoasc output.mp4",shell=True)
